package com.sdp.wz.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wz.db.wz_vendor_wz.entity.WzVendorWz;
import com.sdp.wz.db.wz_vendor_wz.service.IWzVendorWzService;
import com.sdp.wz.db.wz_vendor_wz.service.impl.WzVendorWzServiceImpl;
import com.sdp.wz.view.WzVendorWzView;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-08-02
 */
@RestController
@RequestMapping("/WzVendorWzController")
public class WzVendorWzController {
	
	@Autowired
	private Query query;
	
	@Autowired
	private WzVendorWzServiceImpl wzVendorWzServiceImpl;
	
	@Autowired
	private IWzVendorWzService iWzVendorWzService;
	
	@PostMapping(value = "/viewPage")
	public Result<?> viewPage(@RequestBody DataCenter dataCenter) {
		QueryWrapper<WzVendorWzView> queryWrapper = new QueryWrapper<>();
		return Result.ok(query.viewPage(wzVendorWzServiceImpl, WzVendorWzView.class, dataCenter, queryWrapper));
	}
	
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody JSONObject obj) {
		if(obj== null || obj.get("wzList")==null || StringUtils.isEmpty(obj.getString("warehouseCode")) || StringUtils.isEmpty(obj.getString("vendorCode"))) {
			return Result.error("保存的信息不完整");
		}
		
		//删除原来的
		LambdaQueryWrapper<WzVendorWz> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(WzVendorWz::getWarehouseCode, obj.getString("warehouseCode"));
		queryWrapper.eq(WzVendorWz::getVendorCode, obj.getString("vendorCode"));
		iWzVendorWzService.remove(queryWrapper);
		
		List<WzVendorWz> vendorWzList = JSONArray.parseArray(JSON.toJSONString(obj.get("wzList")), WzVendorWz.class);
		iWzVendorWzService.saveBatch(vendorWzList);
		return Result.ok();
	}
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		iWzVendorWzService.removeById(keyId);
		return Result.ok();
	}
	
}

